%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This file is part of the book
%%
%% Sage for High School
%% http://code.google.com/p/high-school-sage/
%%
%% Copyright (C) 2010 Minh Van Nguyen <nguyenminh2@gmail.com>
%%
%% See the file COPYING for copying conditions. See the file LICENSE
%% for the terms under which the whole book is licensed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{Algebraic Simplification}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Collect like terms}
\index{like terms}

In simplifying an algebraic expression, a basic technique is
identifying like terms\index{like terms}, simplify them and in the
process we also simplify the whole expression. Two like terms can be
simplified to one term because only like terms can be added or
subtracted. The expression
%
\begin{equation}
\label{eq:algebraic_simplify:has_like_terms}
5x + 12x + 17y
\end{equation}
%
can be simplified because $5x$ and $12x$ are like
terms. Contrast~(\ref{eq:algebraic_simplify:has_like_terms}) with
\[
3x + 7y
\]
which has no like terms and cannot be simplified any further.

\begin{lstlisting}
sage: x, y = var("x, y")
sage: 5*x + 12*x + 17*y
17*x + 17*y
sage: simplify(5*x + 12*x + 17*y)
17*x + 17*y
sage: 3*x + 7*y
3*x + 7*y
sage: simplify(3*x + 7*y)
3*x + 7*y
sage: a, b, m, n = var("a, b, m, n")
sage: a + b^2 - 4*a*b + 3*a - b^2 - 2*a*b
-6*a*b + 4*a
sage: 3*(x^2)*(y^3) + 4*x*(y^2) - 9*(y^3)*(x^2) - 7*(y^2)*x
-6*x^2*y^3 - 3*x*y^2
\end{lstlisting}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Distributive laws}
\index{distributive laws}

An expression such as
%
\begin{equation}
\label{eq:algebraic_simplify:simplify_with_left_distributive_law}
7 \times (9 + 1)
\end{equation}
%
can be calculated in various ways. We can first simplify the
expression within the parentheses to get $10$, then multiply this
result with the number outside the parentheses to get $70$:
\[
7 \times (9 + 1)
=
7 \times 10
=
70.
\]
An alternative method for
simplifying~(\ref{eq:algebraic_simplify:simplify_with_left_distributive_law})
is via the left distributive law\index{distributive law!left}, also
called ``expanding the brackets.'' Take the term to the left
of~(\ref{eq:algebraic_simplify:simplify_with_left_distributive_law})
and multiply that with each term inside the parentheses. Add up the
result to get a simplified expression:
\[
7 \times (9 + 1)
=
7 \times 9 + 7 \times 1
=
63 + 7
=
70.
\]
%
\begin{lstlisting}
sage: 7 * (9 + 1)
70
sage: 7*9 + 7*1
70
\end{lstlisting}
%
The left distributive law\index{distributive law!left} is
%
\begin{equation}
\label{eq:algebraic_simplify:left_distributive_law}
\begin{aligned}
a(b + c) &= ab + ac \\
a(b + c + d) &= ab + ac + ad.
\end{aligned}
\end{equation}

\begin{lstlisting}
sage: a, b, c = var("a, b, c")
sage: expand(a * (b + c))
a*b + a*c
sage: bool(a * (b + c) == a*b + a*c)
True
\end{lstlisting}

The right distributive law\index{distributive law!right} works
similarly to its left counterpart
in~(\ref{eq:algebraic_simplify:left_distributive_law}):
%
\begin{equation}
\label{eq:algebraic_simplify:right_distributive_law}
\begin{aligned}
(b + c)a &= ab + ac \\
(b + c + d)a &= ab + ac + ad.
\end{aligned}
\end{equation}
%
We take the term to the right of the parentheses, multiply it with
each term inside the parentheses, finally summing to get a simplified
expression. For example,
\[
(5 + 3) \times 9
=
5 \times 9 + 3 \times 9
=
45 + 27
=
72.
\]

\begin{lstlisting}
sage: (5 + 3) * 9
72
sage: (5*9) + (3*9)
72
sage: a, b, c = var("a, b, c")
sage: expand((b + c) * a)
a*b + a*c
\end{lstlisting}

The rules~(\ref{eq:algebraic_simplify:left_distributive_law})
and~(\ref{eq:algebraic_simplify:right_distributive_law}) are
collectively referred to as the distributive laws\index{distributive laws}.
These rules can be repeatedly applied to simplify expressions such as
\[
7(4x - 3) - 3(3 - 5x)
\]
and
\[
7b(b^2 + 2b - 3) + 3b(b^2 - 4b + 1).
\]

\begin{lstlisting}
sage: x, b = var("x, b")
sage: # first simplify 7(4x - 3) - 3(3 - 5x)
sage: f = expand(7*(4*x - 3)); f
28*x - 21
sage: g = expand(3*(3 - 5*x)); g
-15*x + 9
sage: simplify(f - g)
43*x - 30
sage: # now simplify 7b(b^2 + 2b - 3) + 3b(b^2 - 4b + 1)
sage: f = expand(7*b*(b^2 + 2*b - 3)); f
7*b^3 + 14*b^2 - 21*b
sage: g = expand(3*b*(b^2 - 4*b + 1)); g
3*b^3 - 12*b^2 + 3*b
sage: simplify(f + g)
10*b^3 + 2*b^2 - 18*b
\end{lstlisting}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Binomial expansions}
\index{binomial expansion}

To expand an expression of the form $(a + b)(c + d)$, we repeatedly
use the distributive laws as follows:
%
\begin{equation}
\label{eq:algebraic_simplify:binomial_expansion}
\begin{aligned}
(a + b)(c + d)
&= a(c + d) + b(c + d) \\
&= ac + ad + bc + bd.
\end{aligned}
\end{equation}

\begin{lstlisting}
sage: var("a, b, c, d");
sage: expand((a + b) * (c + d))
a*c + a*d + b*c + b*d
\end{lstlisting}

Carefully inspect the above Sage code listing. We used the semicolon
``\texttt{;}'' to suppress the output of the command \texttt{var}. The
expression~(\ref{eq:algebraic_simplify:binomial_expansion}) is called
a \emph{binomial expansion}\index{binomial expansion}. Here is an
example on how to
use~(\ref{eq:algebraic_simplify:binomial_expansion}) to simplify
$(x + 3)(x + 5)$:

\begin{lstlisting}
sage: f = expand(x*(x + 5))
sage: g = expand(3*(x + 5))
sage: f + g
x^2 + 8*x + 15
sage: expand((x + 3) * (x + 5))
x^2 + 8*x + 15
\end{lstlisting}

Here's a slightly more complicated expression $(3x + 2y)(7p - 3q)$:

\begin{lstlisting}
sage: var("p, q, x, y");
sage: a = 3*x; b = 2*y; c = 7*p; d = -3*q
sage: a*c + a*d + b*c + b*d
21*p*x + 14*p*y - 9*q*x - 6*q*y
sage: expand((3*x + 2*y) * (7*p - 3*q))
21*p*x + 14*p*y - 9*q*x - 6*q*y
\end{lstlisting}

To expand and simplify an expression such as $(a + b) (c + d) (e + f)$,
we use~(\ref{eq:algebraic_simplify:binomial_expansion}) to expand and
simplify the last two factors. We then
reapply~(\ref{eq:algebraic_simplify:binomial_expansion}) to finish off
the job:
%
\begin{equation}
\begin{aligned}
(a + b) (c + d) (e + f)
&=
(a + b) (ce + cf + de + df) \\
&=
a(ce + cf + de + df) + b(ce + cf + de + df) \\
&=
ace + acf + ade + adf + bce + bcf + bde + bdf.
\end{aligned}
\end{equation}

\begin{lstlisting}
sage: var("a, b, c, d, e, f");
sage: expand((a + b) * (c + d) * (e + f))
a*c*e + a*c*f + a*d*e + a*d*f + b*c*e + b*c*f + b*d*e + b*d*f
\end{lstlisting}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Difference of two squares}
\index{difference of two squares}

A slight variation on the
rule~(\ref{eq:algebraic_simplify:binomial_expansion}) is a rule called
\emph{difference of two squares}\index{difference of two squares}. In
general, given a binomial of the form $(a + b)(a - b)$,
use~(\ref{eq:algebraic_simplify:binomial_expansion}) to expand our
expression to produce
%
\begin{equation}
\label{eq:algebraic_simplify:difference_of_two_squares}
\begin{aligned}
(a + b)(a - b)
&=
a(a - b) + b(a - b) \\
&=
a^2 - ab + ab - b^2 \\
&=
a^2 - b^2.
\end{aligned}
\end{equation}
%
\begin{lstlisting}
sage: var("a, b");
sage: expand((a + b) * (a - b))
a^2 - b^2
\end{lstlisting}

Here is an example on using the
rule~(\ref{eq:algebraic_simplify:difference_of_two_squares}) to expand
the expression $(5 - 3x)(5 + 3x)$:
%
\begin{lstlisting}
sage: a = 5; b = 3*x
sage: a^2 - b^2
-9*x^2 + 25
sage: expand((5 - 3*x) * (5 + 3*x))
-9*x^2 + 25
\end{lstlisting}

To expand and simplify the expression
$(x + 1)(x + 3) + (3p - 5)(3p + 5)$, we apply both
rules~(\ref{eq:algebraic_simplify:binomial_expansion})
and~(\ref{eq:algebraic_simplify:difference_of_two_squares}):

\begin{lstlisting}
sage: var("x, p");
sage: # use binomial expansion
sage: A = expand(x*(x + 3))
sage: B = expand(1*(x + 3))
sage: C = A + B; C
x^2 + 4*x + 3
sage: # use difference of two squares
sage: a = 3*p; b = 5
sage: H = a^2 - b^2; H
9*p^2 - 25
sage: C + H
9*p^2 + x^2 + 4*x - 22
sage: expand((x + 1)*(x + 3) + (3*p - 5)*(3*p + 5))
9*p^2 + x^2 + 4*x - 22
\end{lstlisting}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Perfect squares and perfect cubes}
\index{perfect cubes}
\index{perfect squares}

Yet another variation on~(\ref{eq:algebraic_simplify:binomial_expansion})
is a rule called \emph{perfect squares}\index{perfect squares}. In the
binomial $(a + b)(c + d)$, if the factor $(a + b)$ is the same as
$(c + d)$, then we have a perfect square:
%
\begin{equation}
\label{eq:algebraic_simplify:perfect_squares_plus}
\begin{aligned}
(a + b) (a + b)
&=
(a + b)^2 \\
&=
a(a + b) + b(a + b) \\
&=
a^2 + ab + ab + b^2 \\
&=
a^2 + 2ab + b^2.
\end{aligned}
\end{equation}
%
Similarly, use binomial expansion to derive the following variation
on~(\ref{eq:algebraic_simplify:perfect_squares_plus}):
%
\begin{equation}
\label{eq:algebraic_simplify:perfect_squares_minus}
\begin{aligned}
(a - b) (a - b)
&=
(a - b)^2 \\
&=
a(a - b) - b(a - b) \\
&=
a^2 - ab - ab + b^2 \\
&=
a^2 - 2ab + b^2.
\end{aligned}
\end{equation}

\begin{lstlisting}
sage: var("a, b");
sage: expand((a + b)^2)
a^2 + 2*a*b + b^2
sage: expand((a - b)^2)
a^2 - 2*a*b + b^2
\end{lstlisting}

Let's use the rule of perfect squares to simplify $(4x + 7y)^2$:

\begin{lstlisting}
sage: var("x, y");
sage: a = 4*x; b = 7*y
sage: a^2 + 2*a*b + b^2
16*x^2 + 56*x*y + 49*y^2
sage: expand((4*x + 7*y)^2)
16*x^2 + 56*x*y + 49*y^2
\end{lstlisting}

Consider again the expressions $(a + b)^2$ and $(a - b)^2$. Changing
the exponent from $2$ to $3$ and we end up with expressions called
\emph{perfect cubes}\index{perfect cubes}. Perfect cubes have the
forms $(a + b)^3$ and $(a - b)^3$. Use
rule~(\ref{eq:algebraic_simplify:perfect_squares_plus}) and binomial
expansion to simplify the perfect cube $(a + b)^3$ as follows:
%
\begin{equation}
\label{eq:algebraic_simplify:perfect_cubes_plus}
\begin{aligned}
(a + b)^3
&=
(a + b)(a + b)(a + b) \\
&=
(a + b)(a + b)^2 \\
&=
(a + b)(a^2 + 2ab + b^2) \\
&=
a(a^2 + 2ab + b^2) + b(a^2 + 2ab + b^2) \\
&=
a^3 + 2a^2b + ab^2 + a^2b + 2ab^2 + b^3 \\
&=
a^3 + 3a^2b + 3ab^2 + b^3.
\end{aligned}
\end{equation}
%
Similarly, we use binomial expansion
and~(\ref{eq:algebraic_simplify:perfect_squares_minus}) to simplify
the perfect cube $(a - b)^3$:
%
\begin{equation}
\label{eq:algebraic_simplify:perfect_cubes_minus}
\begin{aligned}
(a - b)^3
&=
(a - b)(a - b)(a - b) \\
&=
(a - b)(a^2 - 2ab + b^2) \\
&=
a(a^2 - 2ab + b^2) - b(a^2 - 2ab + b^2) \\
&=
a^3 - 2a^2b + ab^2 - a^2b + 2ab^2 - b^3 \\
&=
a^3 - 3a^2b + 3ab^2 - b^3.
\end{aligned}
\end{equation}

\begin{lstlisting}
sage: var("a, b");
sage: expand((a + b)^3)
a^3 + 3*a^2*b + 3*a*b^2 + b^3
sage: expand((a - b)^3)
a^3 - 3*a^2*b + 3*a*b^2 - b^3
\end{lstlisting}

Here we use~(\ref{eq:algebraic_simplify:perfect_cubes_plus})
and~(\ref{eq:algebraic_simplify:perfect_cubes_minus}) to expand and
simplify the perfect cubes $(2x + 3y)^3$ and $(x - 5y)^3$:

\begin{lstlisting}
sage: var("x, y");
sage: # addition form of perfect cubes
sage: a = 2*x; b = 3*y
sage: a^3 + 3*a^2*b + 3*a*b^2 + b^3
8*x^3 + 36*x^2*y + 54*x*y^2 + 27*y^3
sage: expand((2*x + 3*y)^3)
8*x^3 + 36*x^2*y + 54*x*y^2 + 27*y^3
sage: # subtraction form of perfect cubes
sage: a = x; b = 5*y
sage: a^3 - 3*a^2*b + 3*a*b^2 - b^3
x^3 - 15*x^2*y + 75*x*y^2 - 125*y^3
sage: expand((x - 5*y)^3)
x^3 - 15*x^2*y + 75*x*y^2 - 125*y^3
\end{lstlisting}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Rational expressions}
\index{rational expression}

A rational number is any number that can be written as a
fraction. Similarly, a
\emph{rational expression}\index{rational expression} is a fraction whose
numerator and denominator are both algebraic expressions. Examples of
rational expressions include
\[
\frac{1}{x},
\qquad
\frac{x + 3}{2},
\qquad
\frac{x^2}{(y - 2)^3}.
\]


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Adding and subtracting rational expressions}
\index{rational expression!addition}
\index{rational expression!subtraction}

In arithmetic, to add or subtract two fractions, we first express the
fractions with the same denominator. The same idea applies when we
want to add or subtract two rational
expressions\index{rational expression!addition}.

For example, to add the rational expression
\[
\frac{x + 2}{3} + \frac{x + 3}{5}
\]
first we need to write the fractions with the same
denominator. Multiply $\frac{x + 2}{3}$ by $\frac{5}{5}$, and multiply
$\frac{x + 3}{5}$ by $\frac{3}{3}$. Then go ahead with adding the
resulting fractions. If necessary, use the distributive laws and
binomial expansion to simplify the result:
%
\begin{align*}
\frac{x + 2}{3} + \frac{x + 3}{5}
&=
\frac{5}{5} \times \frac{x + 2}{3} + \frac{3}{3} \times \frac{x + 3}{5} \\[4pt]
&=
\frac{5(x + 2)}{15} + \frac{3(x + 3)}{15} \\[4pt]
&=
\frac{5x + 10}{15} + \frac{3x + 9}{15} \\[4pt]
&=
\frac{8x + 19}{15}.
\end{align*}
%
We can also calculate the numerator and denominator as follows:

\begin{lstlisting}
sage: expand(5*(x + 2) + 3*(x + 3))
8*x + 19
sage: 5*3
15
\end{lstlisting}

Subtracting\index{rational expression!subtraction} two rational
expressions involves the same process: we need to write the two
fractions with a common denominator. Here's a slightly more
complicated rational expression:
\[
\frac{3x}{x - 2} + \frac{y + 5}{x + 3}.
\]
Again, we need to first write both fractions with a common
denominator. Next, use the distributive laws and, if necessary,
binomial expansion to simplify our result:
%
\begin{align*}
\frac{3x}{x - 2} + \frac{y + 5}{x + 3}
&=
\frac{x + 3}{x + 3} \times \frac{3x}{x - 2}
+
\frac{x - 2}{x - 2} \times \frac{y + 5}{x + 3} \\[4pt]
&=
\frac{3x(x + 3)}{(x - 2)(x + 3)}
+
\frac{(y + 5)(x - 2)}{(x + 3)(x - 2)} \\[4pt]
&=
\frac{3x^2 + 9x}{x^2 + x - 6} + \frac{xy + 5x - 2y - 10}{x^2 + x - 6} \\[4pt]
&=
\frac{3x^2 + xy + 14x - 2y - 10} {x^2 + x - 6}.
\end{align*}
%
We can also calculate the numerator and denominator as follows:

\begin{lstlisting}
sage: var("x, y");
sage: expand(3*x*(x + 3) + (y + 5)*(x - 2))
3*x^2 + x*y + 14*x - 2*y - 10
sage: expand((x - 2) * (x + 3))
x^2 + x - 6
\end{lstlisting}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Multiplying and dividing rational expressions}
\index{rational expression!multiplication}
\index{rational expression!division}

In arithmetic, we multiply two fractions by first cancelling any
factors common to the numerator and denominator. Then we proceed with
multiplying the remaining terms in the numerator and
denominator. Carrying this process over to rational expressions, we
multiply the expression\index{rational expression!multiplication}
\[
\frac{3a}{5} \times \frac{11}{ab}
\]
as follows:
%
\begin{align*}
\frac{3a}{5} \times \frac{11}{ab}
&=
\frac{3}{5} \times \frac{11}{b} \\[4pt]
&=
\frac{33}{5b}.
\end{align*}
%
And for a slightly more complicated rational expression:
%
\begin{align*}
\frac{(x + 3) (x - 1)} {(x - 4)} \times \frac{(4 - x)} {(x - 1)}
&=
\frac{(x + 3)} {(x - 4)} \times \frac{(4 - x)} {1} \\[4pt]
&=
\frac{(x + 3)(4 - x)} {(x - 4)} \\[4pt]
&=
\frac{-x^2 + x + 12} {x - 4}.
\end{align*}
%
Notice that we first cancelled the factor $(x - 1)$ in the numerator
and denominator. The numerator and denominator can then be computed as
follows:

\begin{lstlisting}
sage: expand((x + 3) * (4 - x))
-x^2 + x + 12
sage: expand((x - 4) * 1)
x - 4
\end{lstlisting}

To divide\index{rational expression!division} one rational expression
by another, we invert the second rational expression and change the
division sign to a multiplication sign. The original division of
rational expressions is now a multiplication of rational
expressions. Say we want to simplify the following expression
\[
\frac{5a}{3} \div \frac{7a}{13}.
\]
The first step is to consider $\frac{7a}{13}$ and flip its numerator
and denominator around to get
\[
\frac{5a}{3} \times \frac{13}{7a}.
\]
Now multiply the last expression as we have discussed above.
